import 'package:get/get.dart';
import 'package:xindong/common/xindong_api_path.dart';
import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/manager/network/xindong_networking.dart';
import 'package:xindong/manager/xindong_payment_singleton.dart';
import 'package:xindong/models/xindong_noble_class.dart';

class XinDongNobleCtr extends GetxController {
  static XinDongNobleCtr get to => Get.find();

  List<XinDongNobleClass> dataList = [];
  XinDongNobleClass? selectClass; //当前选择的model

  bool isSelectClass(XinDongNobleClass selectClass) {
    return this.selectClass?.id == selectClass.id;
  }

  // 1、选择项目
  void setSelectClass(XinDongNobleClass selectClass) {
    this.selectClass = selectClass;
    update(["model"]);
  }

  // 2、调起支付
  void onTapBuy({Function(XinDongNobleClass model)? onTapNoble}) async {
    if (selectClass == null) {
      showToast("请选择会员类型");
      return;
    }
    if (onTapNoble == null) {
      XinDongPaymentSingleton.instance.buyNoble(selectClass!);
    } else {
      onTapNoble(selectClass!);
    }
  }

  // 列表
  Future requestGetList() async {
    _setupDefaultClass();
    final object = await XinDongNetworking.instance.request(
      XinDongApi.nobleList,
      method: XinDongHttpMethod.post,
    );
    if (object.code == XinDongServerStatus.success) {
      List list = object.data;
      dataList = list.map((e) => XinDongNobleClass.fromJson(e)).toList();
      update();
    }
    _setupDefaultClass();
    return Future.value();
  }

  // 设置默认选项
  void _setupDefaultClass() {
    if (selectClass == null && dataList.isNotEmpty) {
      setSelectClass(dataList.first);
    }
  }
}
